"use strict";
cc._RF.push(module, '3a51dGM/YFDlIO3XIm9Z+Gl', 'o1DrawBoard');
// scripts/O1ModuleRes/control/o1DrawBoard.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var o1DrawInterface_1 = require("./o1DrawBoard/o1DrawInterface");
var gameEngine_1 = require("../../engines/gameEngine");
var drawRecord_1 = require("./o1DrawBoard/drawRecord");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var o1DrawBoard = /** @class */ (function (_super) {
    __extends(o1DrawBoard, _super);
    function o1DrawBoard() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.drawBoard = null;
        _this.ndLayerSetColor = null;
        _this.ndEmptyNode = null;
        _this.sdSetWidth = null;
        _this.drawSetBoard = null;
        _this.minLineWidth = 2;
        _this.maxLineWidth = 50;
        _this.editDrawName = null;
        _this.pbRecordList = null;
        _this.m_setColors = [
            cc.color(0, 0, 0),
            //
            //
            cc.color(255, 0, 0),
            cc.color(255, 128, 0),
            cc.color(255, 255, 0),
            cc.color(0, 255, 0),
            cc.color(0, 255, 255),
            cc.color(0, 0, 255),
            cc.color(128, 0, 255),
            //
            cc.color(64, 64, 0),
            cc.color(0, 64, 64),
            cc.color(64, 0, 64),
            cc.color(128, 128, 0),
            cc.color(0, 128, 128),
            cc.color(128, 0, 128),
            //
            cc.color(255, 0, 128),
            cc.color(255, 128, 128),
            cc.color(255, 128, 255),
            //
            cc.color(128, 255, 0),
            // cc.color(0, 255, 128),
            cc.color(128, 255, 128),
            cc.color(128, 255, 255),
            //
            cc.color(128, 128, 255),
        ];
        _this.m_currColor = null;
        _this.m_currLineWidth = null;
        _this.m_drawData = null;
        _this.m_o1DrawClass = null;
        _this.m_moveCount = 0;
        _this.m_updateCount = 0;
        return _this;
        // update (dt) {}
    }
    o1DrawBoard.prototype.onLoad = function () {
        this.drawBoard.node.on('touchstart', this.onTouchStart.bind(this));
        this.drawBoard.node.on('touchmove', this.onTouchMove.bind(this));
        this.drawBoard.node.on('touchend', this.onTouchEnd.bind(this));
        this.drawBoard.node.on('touchcancel', this.onTouchCancel.bind(this));
        //
        gameEngine_1.default.off('o1DrawBoard.OnEditDraw');
        gameEngine_1.default.off('o1DrawBoard.OnDeleteDraw');
        gameEngine_1.default.on('o1DrawBoard.OnEditDraw', this.OnEditDraw.bind(this));
        gameEngine_1.default.on('o1DrawBoard.OnDeleteDraw', this.OnDeleteDraw.bind(this));
        //
        this.onInitSetColor();
        this.onInitSetWidth();
        //
        this.onDrawInit();
    };
    o1DrawBoard.prototype.start = function () {
        this.m_o1DrawClass = new o1DrawInterface_1.default();
        this.m_o1DrawClass.OnLoadData();
    };
    o1DrawBoard.prototype.onBtnSelectColor = function (target) {
        console.log('选择色', target.node.name);
        var i = target.node.name;
        //
        this.drawBoard.strokeColor = this.drawBoard.fillColor = this.m_setColors[i];
        this.drawBoard.lineWidth = this.minLineWidth + this.sdSetWidth.progress * (this.maxLineWidth - this.minLineWidth);
        //
        this.ndEmptyNode.active = false;
        this.onSetPen();
    };
    o1DrawBoard.prototype.onBtnSetWidth = function () {
        this.drawBoard.lineWidth = this.minLineWidth + this.sdSetWidth.progress * (this.maxLineWidth - this.minLineWidth);
        this.drawBoard.strokeColor = this.drawBoard.fillColor = this.m_currColor;
        this.ndEmptyNode.active = false;
        this.onSetPen();
    };
    o1DrawBoard.prototype.onBtnSelectPen = function () {
        this.ndEmptyNode.active = false;
        this.drawBoard.lineWidth = this.m_currLineWidth;
        this.drawBoard.strokeColor = this.drawBoard.fillColor = this.m_currColor;
    };
    o1DrawBoard.prototype.OnBtnSaveData = function () {
        var name = this.editDrawName.string;
        if (!name) {
            gameEngine_1.default.getModules().uiControlModule.showMessageBox('未输入名字');
            return;
        }
        this.SaveDrawInfo(name);
        gameEngine_1.default.getModules().uiControlModule.showMessageBox('已保存');
    };
    o1DrawBoard.prototype.OnBtnLoadData = function () {
        var node = cc.instantiate(this.pbRecordList);
        this.node.addChild(node);
        node.getComponent(drawRecord_1.default).bindDrawInterface(this.m_o1DrawClass);
    };
    o1DrawBoard.prototype.onBtnClear = function () {
        this.editDrawName.string = '未命名';
        this.drawBoard.clear();
        this.OnClearDrawData();
    };
    o1DrawBoard.prototype.onBtnEmpty = function () {
        if (this.ndEmptyNode.active) {
            this.onGetPen();
        }
        else {
            this.drawBoard.lineWidth = this.maxLineWidth * 2;
            this.drawBoard.strokeColor = this.drawBoard.fillColor = this.drawBoard.node.parent.color;
            this.ndEmptyNode.setPosition(this.drawBoard.node.width / 2, this.drawBoard.node.height / 2);
        }
        this.ndEmptyNode.active = !this.ndEmptyNode.active;
    };
    o1DrawBoard.prototype.onTouchStart = function (event) {
        var viewPos = this.drawBoard.node.convertToNodeSpaceAR(event.touch.getLocation());
        viewPos = this.isOutWeb(viewPos);
        this.OnStartDraw(viewPos);
        //
        this.ndEmptyNode.setPosition(viewPos);
        this.m_moveCount = 0;
    };
    o1DrawBoard.prototype.onTouchMove = function (event) {
        this.m_moveCount++;
        var viewPos = this.drawBoard.node.convertToNodeSpaceAR(event.touch.getLocation());
        viewPos = this.isOutWeb(viewPos);
        this.OnIngDraw(viewPos);
        // this.m_moveCount = 0;
        // }
        this.ndEmptyNode.setPosition(viewPos);
    };
    o1DrawBoard.prototype.onTouchEnd = function (event) {
        var viewPos = this.drawBoard.node.convertToNodeSpaceAR(event.touch.getLocation());
        viewPos = this.isOutWeb(viewPos);
    };
    o1DrawBoard.prototype.onTouchCancel = function (event) {
        var viewPos = this.drawBoard.node.convertToNodeSpaceAR(event.touch.getLocation());
        viewPos = this.isOutWeb(viewPos);
    };
    o1DrawBoard.prototype.OnBtnExit = function () {
        this.node.destroy();
    };
    o1DrawBoard.prototype.OnDeleteDraw = function (name) {
        this.m_o1DrawClass.DeleteData(name);
    };
    o1DrawBoard.prototype.OnEditDraw = function (name) {
        this.OnClearDrawData();
        this.drawBoard.clear();
        //
        var data = this.m_o1DrawClass.getData(name);
        if (!data) {
            gameEngine_1.default.getModules().uiControlModule.showMessageBox("\u6CA1\u6709\u627E\u5230\u8BE5\u753B\u4F5C<" + name + ">");
            return;
        }
        //
        this.editDrawName.string = name;
        //
        for (var i = 0; i < data.drawList.length; i++) {
            var _a = data.drawList[i], lineWidth = _a.lineWidth, color = _a.color, x = _a.x, y = _a.y, isStart = _a.isStart;
            var r = color.r, g = color.g, b = color.b;
            //
            this.drawBoard.lineWidth = lineWidth;
            this.drawBoard.strokeColor = cc.color(r, g, b);
            //
            if (isStart) {
                this.OnStartDraw(cc.v2(x, y));
            }
            else {
                this.OnIngDraw(cc.v2(x, y));
            }
        }
    };
    o1DrawBoard.prototype.OnIngDraw = function (viewPos) {
        var x = viewPos.x, y = viewPos.y;
        this.drawBoard.lineTo(x, y);
        this.drawBoard.stroke();
        // if (this.m_moveCount > 30) {
        this.drawBoard.moveTo(x, y);
        //
        var _a = this.drawBoard.strokeColor, r = _a.r, g = _a.g, b = _a.b;
        this.OnAddDrawList({ x: x, y: y, color: { r: r, g: g, b: b }, lineWidth: this.drawBoard.lineWidth, isStart: false });
    };
    o1DrawBoard.prototype.OnStartDraw = function (viewPos) {
        var x = viewPos.x, y = viewPos.y;
        this.drawBoard.moveTo(x - 1, y - 1);
        this.drawBoard.lineTo(x, y);
        this.drawBoard.stroke();
        //
        var _a = this.drawBoard.strokeColor, r = _a.r, g = _a.g, b = _a.b;
        this.OnAddDrawList({ x: x, y: y, color: { r: r, g: g, b: b }, lineWidth: this.drawBoard.lineWidth, isStart: true });
    };
    o1DrawBoard.prototype.OnClearDrawData = function () {
        this.m_drawData = null;
    };
    o1DrawBoard.prototype.OnAddDrawList = function (info) {
        if (this.m_drawData == null) {
            this.m_drawData = { name: null, drawList: [] };
        }
        this.m_drawData.drawList.push(info);
    };
    //保存绘画信息 名字
    o1DrawBoard.prototype.SaveDrawInfo = function (name) {
        var _this = this;
        this.m_drawData.name = name;
        if (this.m_o1DrawClass.getData(name)) {
            gameEngine_1.default.getModules().uiControlModule.showDialogBox('保存', "\u4FDD\u5B58\u4F1A\u8986\u76D6\u4E4B\u524D\u753B\u4F5C,\u662F\u5426\u4FDD\u5B58<" + name + ">", true, function (isOk) {
                if (isOk) {
                    _this.m_o1DrawClass.AddAndWriteDrawData(_this.m_drawData);
                }
            }, null, '保存', '取消');
        }
        else {
            this.m_o1DrawClass.AddAndWriteDrawData(this.m_drawData);
        }
    };
    o1DrawBoard.prototype.update = function (dt) {
        this.m_updateCount++;
        if (this.m_updateCount % 2 == 1) {
            // this.drawBoard.stroke();
        }
    };
    o1DrawBoard.prototype.onSetPen = function () {
        this.m_currColor = this.drawBoard.strokeColor;
        this.m_currLineWidth = this.drawBoard.lineWidth;
        this.onUpdateDrawSetGdi();
    };
    o1DrawBoard.prototype.onGetPen = function () {
        this.drawBoard.strokeColor = this.drawBoard.fillColor = this.m_currColor;
        this.drawBoard.lineWidth = this.m_currLineWidth;
        this.onUpdateDrawSetGdi();
    };
    o1DrawBoard.prototype.onUpdateDrawSetGdi = function () {
        this.drawSetBoard.strokeColor = this.drawBoard.strokeColor;
        this.drawSetBoard.lineWidth = this.drawBoard.lineWidth;
        this.drawSetBoard.clear();
        this.drawSetBoard.moveTo(20, 0);
        this.drawSetBoard.bezierCurveTo(0, 40, 35, 100, 20, 150);
        this.drawSetBoard.stroke();
    };
    //是否出界
    o1DrawBoard.prototype.isOutWeb = function (pos) {
        var outPos = pos.clone();
        if (pos.x > this.drawBoard.node.width - this.drawBoard.lineWidth / 2) {
            outPos.x = this.drawBoard.node.width - this.drawBoard.lineWidth / 2;
        }
        if (pos.x < this.drawBoard.lineWidth / 2) {
            outPos.x = this.drawBoard.lineWidth / 2;
        }
        if (pos.y > this.drawBoard.node.height - this.drawBoard.lineWidth / 2) {
            outPos.y = this.drawBoard.node.height - this.drawBoard.lineWidth / 2;
        }
        if (pos.y < this.drawBoard.lineWidth / 2) {
            outPos.y = this.drawBoard.lineWidth / 2;
        }
        return outPos;
    };
    o1DrawBoard.prototype.onDrawInit = function () {
        this.drawBoard.strokeColor = this.drawBoard.fillColor = this.m_setColors[0];
        this.drawBoard.lineWidth = this.minLineWidth;
        this.ndEmptyNode.active = false;
        this.onSetPen();
    };
    o1DrawBoard.prototype.onInitSetWidth = function () {
        this.sdSetWidth.progress = 0;
    };
    o1DrawBoard.prototype.onInitSetColor = function () {
        for (var i = 0; i < this.m_setColors.length; i++) {
            var currColor = this.m_setColors[i];
            //
            var child = this.ndLayerSetColor.children[0];
            if (i > 0) {
                child = cc.instantiate(this.ndLayerSetColor.children[0]);
                this.ndLayerSetColor.addChild(child);
            }
            child.name = String(i);
            child.getChildByName('Background').color = currColor;
        }
    };
    __decorate([
        property(cc.Graphics)
    ], o1DrawBoard.prototype, "drawBoard", void 0);
    __decorate([
        property(cc.Node)
    ], o1DrawBoard.prototype, "ndLayerSetColor", void 0);
    __decorate([
        property({ type: cc.Node, displayName: '橡皮擦' })
    ], o1DrawBoard.prototype, "ndEmptyNode", void 0);
    __decorate([
        property(cc.Slider)
    ], o1DrawBoard.prototype, "sdSetWidth", void 0);
    __decorate([
        property(cc.Graphics)
    ], o1DrawBoard.prototype, "drawSetBoard", void 0);
    __decorate([
        property({ tooltip: '画笔最小宽度' })
    ], o1DrawBoard.prototype, "minLineWidth", void 0);
    __decorate([
        property({ tooltip: '画笔最大宽度' })
    ], o1DrawBoard.prototype, "maxLineWidth", void 0);
    __decorate([
        property(cc.EditBox)
    ], o1DrawBoard.prototype, "editDrawName", void 0);
    __decorate([
        property(cc.Prefab)
    ], o1DrawBoard.prototype, "pbRecordList", void 0);
    o1DrawBoard = __decorate([
        ccclass
    ], o1DrawBoard);
    return o1DrawBoard;
}(cc.Component));
exports.default = o1DrawBoard;

cc._RF.pop();